public class Convert {
    //Z字形变换
    public static String code(String s, int n) {
        int len = s.length();
        int d = 2 * n - 2;
        StringBuilder ret = new StringBuilder();

        if (n == 1){
            return s;
        }

        //来放第一行的字符
        for (int i = 0; i < len; i += d){
            ret.append(s.charAt(i));
        }

        //放中间行的字符
        for (int k = 1; k < n - 1; k ++){   //k是行数
            for (int i = k, j = d - i; i < len || j < len; i += d, j += d){
                if (i < len){
                    ret.append(s.charAt(i));
                }

                if (j < len){
                    ret.append(s.charAt(j));
                }
            }
        }

        //来放最后一行的字符
        for (int i = n - 1; i < len; i += d){
            ret.append(s.charAt(i));
        }
        //读取
        return ret.toString();
    }

    public static void main(String[] args) {
        Convert.code("A", 1);
    }
}
